package com.zhuannimei.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.zhuannimei.GlobalApplication;
import com.zhuannimei.R;
import com.zhuannimei.utility.MessageHandlerList;
import com.zhuannimei.view.HandyTextView;
import com.zhuannimei.view.LoadingDialog;
import com.zhuannimei.view.TitlePane;

public abstract class BaseFragment extends Fragment {
	public TitlePane mTitlePane;
	FragmentManager superFManager;
	protected View mView;// 父类控件声明
	public LoadingDialog pd;
	protected List<AsyncTask<Void, Void, String>> mAsyncTasks = new ArrayList<AsyncTask<Void, Void, String>>();

	protected GlobalApplication mApplication;
	protected Activity mActivity;
	protected Context mContext;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showImageForEmptyUri(R.drawable.ic_launcher)
			.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
			.cacheOnDisc(true)
			.displayer(new RoundedBitmapDisplayer(1)).build();
	public BaseFragment() {
		super();
	}

	public BaseFragment(GlobalApplication application, Activity activity,
			Context context) {
		mApplication = application;
		mActivity = activity;
		mContext = context;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	public Handler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		superFManager = getActivity().getSupportFragmentManager();
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				handleMsg(msg);
			}
		};
		pd = new LoadingDialog(getActivity());
		// 当前业务类的类名
		String className = this.getClass().getName();
		MessageHandlerList.addHandler(className, mHandler);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		initViews();
		initEvents();
		initData();
		return mView;
	}

	/**
	 * 初始化控件
	 */
	protected abstract void initViews();

	/**
	 * 设置控件监听
	 */
	protected abstract void initEvents();

	/**
	 * 初始化数据
	 */
	protected abstract void initData();

	/**
	 * 查询控件
	 * 
	 * @param id
	 * @return
	 */
	public View findViewById(int id) {
		return mView.findViewById(id);
	}

	/**
	 * 异步任务
	 * 
	 * @param asyncTask
	 */
	protected void putAsyncTask(AsyncTask<Void, Void, String> asyncTask) {
		mAsyncTasks.add(asyncTask.execute());
	}

	/**
	 * 清除异步
	 */
	protected void clearAsyncTask() {
		Iterator<AsyncTask<Void, Void, String>> iterator = mAsyncTasks
				.iterator();
		while (iterator.hasNext()) {
			AsyncTask<Void, Void, String> asyncTask = iterator.next();
			if (asyncTask != null && !asyncTask.isCancelled()) {
				asyncTask.cancel(true);
			}
		}
		mAsyncTasks.clear();
	}

	/** 显示自定义Toast提示(来自String) **/
	protected void showCustomToast(String text) {
		View toastRoot = LayoutInflater.from(getActivity()).inflate(
				R.layout.common_toast, null);
		((HandyTextView) toastRoot.findViewById(R.id.toast_text)).setText(text);
		Toast toast = new Toast(getActivity());
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(toastRoot);
		toast.show();
	}

	//

	protected void handleMsg(Message msg) {
	}

	@Override
	public void onDetach() {
		super.onDetach();
	}

	@Override
	public void onStart() {
		super.onStart();
		mTitlePane = new TitlePane(BaseFragment.this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		clearAsyncTask();
		String className = this.getClass().getName();
		MessageHandlerList.removeHandler(className);
	}
}
